//
//  EPMNaviTitleView.m
//  myXianfeng
//
//  Created by 马继鵬 on 17/5/1.
//  Copyright © 2017年 EPM. All rights reserved.
//

#import "EPMNaviTitleView.h"
#import "EPMSearchViewController.h"
#import "EPMScanViewController.h"

@interface EPMNaviTitleView()
#pragma mark - 渲染参数 -
@property (weak,nonatomic) UIControl* scanControl;
@property (weak,nonatomic) UILabel* scanImg;
@property (weak,nonatomic) UIControl* searchControl;
@property (weak,nonatomic) UILabel* searchImg;
@property (weak,nonatomic) UIControl* titleControl;
@property (weak,nonatomic) UILabel* desLabel;
@property (weak,nonatomic) UILabel* addressLabel;
@property (weak,nonatomic) UILabel* arrowImg;

@end
@implementation EPMNaviTitleView


#pragma mark - 添加导航栏按键 -
- (instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        [self addBtns];
    }
    return self;
}


- (void)addBtns{
    
   
    
    UIControl* scanControl = [[UIControl alloc]initWithFrame:CGRectMake(20, 25, 30, 30)];
    self.scanControl = scanControl;
    scanControl.backgroundColor = [UIColor colorWithWhite:120/255.0 alpha:0.4];
    scanControl.layer.masksToBounds = YES;
    scanControl.layer.cornerRadius = 15;
    [self addSubview:scanControl];
    [scanControl addTarget:self action:@selector(actionScan:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel* imgView = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 20, 20)];
    self.scanImg = imgView;
    imgView.text = Scan;
    imgView.font =  [UIFont fontWithName:@"iconfont" size:20];
    [scanControl addSubview:imgView];
    
    UIControl* searchControl = [[UIControl alloc]initWithFrame:CGRectMake(ScreenWidth-70, 25, 30, 30)];
    self.searchControl = searchControl;
    searchControl.backgroundColor = [UIColor colorWithWhite:120/255.0 alpha:0.4];
    searchControl.layer.masksToBounds = YES;
    searchControl.layer.cornerRadius = 15;
    [self addSubview:searchControl];
    [searchControl addTarget:self action:@selector(actionSearch:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel* searchImgView = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 20, 20)];
    self.searchImg = searchImgView;
    searchImgView.text = Search;
    searchImgView.font = [UIFont fontWithName:@"iconfont" size:20];
    [searchControl addSubview:searchImgView];
    
    
    [self setupTitleView];
    [self setTintalpha:0.0];
}
#pragma mark - 配送地址 -


- (void)setupTitleView{
    UIControl* titleView = [[UIControl alloc]initWithFrame:CGRectMake(ScreenWidth/2-100, 25, 200, 30)];
    self.titleControl = titleView;
    titleView.backgroundColor = [UIColor colorWithWhite:120/255.0 alpha:0.4];
    titleView.layer.masksToBounds = YES;
    titleView.layer.cornerRadius = 15;
    
    UILabel* desLabel = [[UILabel alloc]init];
    self.desLabel = desLabel;
    desLabel.text = @"配送至";
    desLabel.font = [UIFont systemFontOfSize:10];
    [titleView addSubview:desLabel];
    
    UILabel* deafultLabel = [[UILabel alloc]init];
    self.addressLabel = deafultLabel;
    deafultLabel.text = @"你在哪里呀";
    deafultLabel.font = [UIFont systemFontOfSize:15];
    [titleView addSubview:deafultLabel];
    
    UILabel* imgView = [[UILabel alloc]init];
    self.arrowImg = imgView;
    imgView.text = Rectangle;
    imgView.font = [UIFont fontWithName:@"iconfont" size:20];
    [titleView addSubview:imgView];
    
    [desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(deafultLabel.mas_leading).offset(-7);
        make.baseline.equalTo(deafultLabel);
    }];
    
    [deafultLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(titleView.mas_centerX).offset(-20);
        make.centerY.equalTo(titleView);
    }];
    
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(deafultLabel.mas_trailing).offset(7);
        make.centerY.equalTo(deafultLabel);
        //        make.trailing.equalTo(titleView);
    }];
    
    [self addSubview:titleView];
    
}
#pragma mark - 设置navigationbar的颜色 -

- (void)setTintalpha:(CGFloat)alpha{
    
    self.backgroundColor = [UIColor colorWithRed:254/255.0 green:213/255.0 blue:48/255.0 alpha:alpha];
    UIColor* color = alpha > 0.5 ? [UIColor blackColor] :[UIColor whiteColor];
    self.scanImg.textColor = color;
    self.searchImg.textColor = color;
    self.desLabel.textColor = color;
    self.addressLabel.textColor = color;
    self.arrowImg.textColor = color;
    
}

- (void)setDeafultColor{
    self.backgroundColor = [UIColor whiteColor];
    UIColor* color = [UIColor blackColor] ;
    self.searchImg.textColor = [UIColor grayColor];
    self.desLabel.textColor = color;
    self.addressLabel.textColor = color;
    self.arrowImg.textColor = color;
    self.scanControl.hidden = YES;
}

#pragma mark - 点击事件 -
//扫一扫
- (void)actionScan:(id)sender{
    //模拟器不支持
    
    [self.controller.navigationController pushViewController:[EPMScanViewController new] animated:YES];
}
//搜索
- (void)actionSearch:(id)sender{
    [self.controller.navigationController pushViewController:[EPMSearchViewController new] animated:YES];
}

@end
